package com.itheima.controller;

import com.itheima.domain.Manager;
import com.itheima.domain.Student;
import com.itheima.factory.Factory;
import com.itheima.service.IManagerService;
import com.itheima.util.ExportExcel;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Scanner;

public class ImplManagerController implements IManagerController {
    private IManagerService managerService = Factory.getManagerService();
    Scanner sc = new Scanner(System.in);
    private static final Logger LOGGER = LoggerFactory.getLogger(IManagerController.class);
    private static Manager manager;

    @Override
    public void start() throws IOException {

        while (true) {
            System.out.println("========欢迎来到黑马信息管理系统！========");
            System.out.println("请输入您的选择：1.添加学生 2.删除学生 3.修改学生 4.查看学生 5.报表统计 6.导出信息 7.查看日志 8.退出");
            String choice = sc.next();
            switch (choice) {
                case "1":
                    Factory.getStudentController().addStudent();
                    break;
                case "2":
                    Factory.getStudentController().deleteStudent();
                    break;
                case "3":
                    Factory.getStudentController().updateStudent();
                    break;
                case "4":
                    Factory.getStudentController().findAllStudent();
                    break;
                case "5":
                    Factory.getStudentController().show();
                    break;
                case "6":
                    toExcel();
                    break;
                case "7":
                    lookLogger();
                    break;
                case "8":
                    System.out.println("感谢您的使用");
                    return;
                default:
                    System.out.println("输入有误,请重新输入");
                    break;
            }
        }
    }


    @Override
    public void manLogin() {


    }

    @Override
    public void register() {
        while (true) {
            System.out.println("请输入id：");
            String id;
            id = sc.nextLine();
            if (managerService.isExists(id)) {
                break;
            } else {
                System.out.println("账号已经存在，重新录入");
            }
        }
    }

    @Override
    public void updatePassword() {
        System.out.println("请输入账号：");
        String id = sc.next();
        Manager man = managerService.findMan(id);
        if (man == null) {
            System.out.println("请先注册账号！");
            return;
        }
        System.out.println("请输入绑定的邮箱：");
        String email;
        String s;
        while (true) {
            email = sc.next();
            System.out.println("验证码已发出，请查看");

        }

    }
    private void toExcel() {
        long start = System.currentTimeMillis();
        HSSFWorkbook weebBook = new HSSFWorkbook();
        ArrayList<Student> students = Factory.getStudentDao().findAllStudent();
        String[] arr = {"序号", "学号", "密码", "姓名", "性别", "年龄", "班级", "组号", "手机号", "生日"};
        for (int i = 0; i < 2; i++) {
            HSSFSheet sheet = weebBook.createSheet("页签" + i);
            ArrayList<Object[]> strings = new ArrayList<>();
            for (int i1 = 0; i1 < students.size(); i1++) {
                strings.add(new String[]{String.valueOf(i), students.get(i1).getId(),  students.get(i1).getName(), students.get(i1).getGender(), String.valueOf(students.get(i1).getAge()), students.get(i1).getClasses(), String.valueOf(students.get(i1).getGroup()), students.get(i1).getPhone(), String.valueOf(students.get(i1).getBirthday())});
            }
            ExportExcel excel = new ExportExcel("学生信息", arr, strings);
            try {
                excel.export(weebBook, sheet, i);
            } catch (Exception e) {
                System.out.println("文件路径名未指定错误!");
            }
        }
        long end = System.currentTimeMillis();
        long time = end - start;
        LOGGER.info("管理员" + manager.getId() + ",在" + LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy年MM月dd日 HH:mm:ss")) + "导出学生信息到excel表格中,用时" + time + "ms");
        System.out.println("导出学生信息成功!");
    }

    @Override
    public void lookLogger() {
        ArrayList<String> loggerList = managerService.lookLogger();
        System.out.println("日志内容是： ");
        loggerList.stream().filter(s -> s.contains(manager.getId())).forEach(s -> System.out.println(s));

    }
}